<script setup lang="ts">
  import { ref, defineProps, reactive, defineExpose } from "vue"
  import { createMessage } from '@/Public-UIlibrary/pb-message/method'
  import { getConfigKitchen, getCenterKitchen } from '@/api/System'
  // 获取树装区域列表
  import { getTenantAreaTree } from '@/api/System'
  let emits = defineEmits('onConfirmModal')
  const props = defineProps({
    tenantTreeData: {
      type: Array,
      default: () => []
    }
  })
  const auitTypelist = [{
    value: 'auto',
    lable: '自动'
  },
  {
    value: 'manual',
    lable: '手动'
  }
  ]
  let loading = ref(false)
  const initialFrom = {
    areaId: '',

    tenantId: "",

    recipePlanExamineType: "",
  }
  let tenantConfigFrom = ref({
    ...initialFrom
  })
  const areaTree = ref([])
  const tenantConfigFromRef = ref()
  let LevelModalShow = ref(false)

  const onShow = (row) => {
    if (row) {
      getCenterKitchenApi(row.tenantId)
    }

    LevelModalShow.value = true
  }
  // 关闭表单重置
  const cancelFn = () => {
    tenantConfigFromRef.value.resetFields();
    Object.assign(tenantConfigFrom.value, initialFrom);
  }
  const onConfirmFn = () => {
    console.log('确认');

    tenantConfigFromRef.value.validate((valid) => {
      if (valid) {
        getConfigKitchenApi()


      } else {

      }
    })
  }
  // 获取层级树接口
  const getTenantAreaTreeApi = async () => {
    let res = await getTenantAreaTree({})
    if (res.code == 2000) {
      areaTree.value = [res.data]
      console.log(areaTree);
    }
  }
  // 添加组织架构 getCenterKitchen
  const getCenterKitchenApi = async (tenantId) => {
    loading.value = true
    let res = await getCenterKitchen({ tenantId })
    loading.value = false
    if (res.code == 2000) {
      tenantConfigFrom.value = { ...res.data, tenantId }
      console.log(tenantConfigFrom.value);

    }


  }
  // 编辑组织架构
  const getConfigKitchenApi = async () => {
    loading.value = true
    let res = await getConfigKitchen({ ...tenantConfigFrom.value })
    loading.value = false
    if (res.code == 2000) {
      createMessage({
        message: '编辑成功',
        type: 'success',
      })
      LevelModalShow.value = false
      emits('onConfirmModal')
    }
  }
  onMounted(() => {
    getTenantAreaTreeApi()
  })
  defineExpose({ onShow })
</script>

<template>
  <pb-popup v-model="LevelModalShow" title="配置" Width="368" @confirmFn="onConfirmFn" @cancelFn="cancelFn">
    <el-form :model="tenantConfigFrom" ref="tenantConfigFromRef" label-width="auto" v-loading="loading">

      <el-form-item label="所属区域" prop="areaId" :rules="[
        {
          required: true,
          message: '请选择所属区域',
          trigger: 'blur',
        }, ]">
        <pb-tree-select v-model="tenantConfigFrom.areaId" :Data="areaTree" width="240"
          :defaultProps="{ children: 'children', label: 'areaName', value: 'areaId' }" @change="getSpuInfoListApi" />
      </el-form-item>
      <el-form-item label="食谱审核" prop="recipePlanExamineType">
        <pb-radio-group :optionList='auitTypelist' v-model="tenantConfigFrom.recipePlanExamineType"></pb-radio-group>
      </el-form-item>
    </el-form>
  </pb-popup>
</template>

<style lang="less" scoped>

</style>